# -*-coding:Utf-8 -*
""" UI Input Text Class
"""
import bge, bgl, blf
from .textbox import *
from .buttonlist import *
from .image import *
	
texTable=[["checkout","check"],["checkout","checkonce"]]
checkTab=dict()
	
class CheckBox(TextBox):
	def __init__(self, parent,x, y, value, onclick=False, label="", checked=False, mode=0, id=False, **kargs): #mode=0: ckeck, mode=1: radio
		TextBox.__init__(self, parent,x, y, label)	
		self._font=ButtonList.DefaultFont
		self._size=ButtonList.DefaultSize
		self.parent=parent
		self.imgBox=Image('//resources/lib/gui/{}.png'.format(texTable[mode][int(checked)]))
		self.checkBox=Box(parent, x, y,self.imgBox.width*(self.h/self.imgBox.height),self.h,self.imgBox)
		self._setx(x+self.imgBox.width*(self.h/self.imgBox.height)+4)
		self.checkBox._onclic=self.changeState
		self._onclic=self.changeState
		self.actClic=onclick
		self.mode=mode
		self.stat=checked
		if self.mode==0 and id is not False:
			if id not in checkTab:
				checkTab[id]=[self]
			else:
				checkTab[id].append(self)
				
	def isChecked(self):
		return self.stat
		
	def changeState(self, params):
		if self.actClic is not False:
			stat=self.stat			
			if self.actClic:
				self.actClic()
		if self.mode==0:
			self.imgBox.unload()
			self.imgBox=Image('//resources/lib/gui/{}.png'.format(texTable[self.mode][int(not self.stat)]))
			self.stat=not self.stat
		else:
			if self.stat is False and id in checkTable:
				for elem in checkTab[self.idCheck][id]:
					if elem.value==self.value:
						self.imgBox.unload()
						self.imgBox=Image('//resources/lib/gui/{}.png'.format(texTable[self.mode][int(True)]))
						self.stat=True
					else:
						elem.imgBox.unload()
						elem.imgBox=Image('//resources/lib/gui/{}.png'.format(texTable[self.mode][int(False)]))
						elem.stat=False

